package com.barney.support.jpa;

import java.util.Map;

import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.google.common.collect.Maps;

/**
 * Hibernate를 사용하여 생성되는 Schema 내보내기.
 * <p>
 * <code>META-INF/persistence.xml</code>에 지정된 name 값을 사용하여 Schema를 콘솔로 출력한다.
 * <blockquote>
 *
 * <pre>
 * 예 1:
 * &lt;persistence-unit name="my-pu" ..&gt;
 * ..
 * &lt;/persistence-unit&gt;
 * </pre>
 *
 * </blockquote>
 * </p>
 *
 * <blockquote>
 *
 * <pre>
 * 실행 예:
 * java HibernateSchemaExport my-pu
 * </pre>
 *
 * </blockquote>
 *
 * @see <a href=
 *      "http://hillert.blogspot.com/2010/05/using-hibernates-schemaexport-feature.html"
 *      >Using Hibernate's SchemaExport Feature from within a Spring/JPA
 *      Context</a>
 */
public class HibernateSchemaExport {

	public HibernateSchemaExport() {
		//
	}

	public void print(String persistenceUnitName) {
		Ejb3Configuration cfg = new Ejb3Configuration();
		Map<String, String> integration = Maps.newHashMap();
		integration.put("hibernate.format_sql", "true");
		Ejb3Configuration configured = cfg.configure(persistenceUnitName,
				integration);
		SchemaExport schemaExport = new SchemaExport(
				configured.getHibernateConfiguration());
		schemaExport.setDelimiter(";");
		schemaExport.create(true, false);
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			System.err
					.println("Usage: HibernateSchemaExport persistenceUnitName");
			System.exit(1);
		}
		HibernateSchemaExport export = new HibernateSchemaExport();
		export.print(args[0]);
	}
}
